package com.javajun.endofterm.controller.api_v1;

import com.javajun.endofterm.common.AjaxUtils;
import com.javajun.endofterm.common.DataGridParam;
import com.javajun.endofterm.common.DataGridUtils;
import com.javajun.endofterm.entity.Domitory;
import com.javajun.endofterm.service.DomitoryService;
import com.javajun.endofterm.service.StudentService;
import com.javajun.endofterm.service.SysUserService;
import com.javajun.endofterm.web.form.DomitoryForm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Xiong Limin
 * Date: 2020-12-01-10:05 下午
 */
@RequestMapping("/api-v1/domitory")
@RestController
public class DomitoryController {

    @Autowired
    private DomitoryService domitoryService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "/page")
    public Object page(DataGridParam param) {
        if (param.getLimit() == null) {
            param.setLimit(15);
        }
        if (param.getPage() == null) {
            param.setPage(1);
        }

        Sort sort = Sort.by("id");
        Pageable pageable = param.getPageable(sort);
        Page<Domitory> page = domitoryService.findAll(pageable);
        return DataGridUtils.buildResult(page);
    }

    @RequestMapping(value = "/search")
    public Object search(DataGridParam param, DomitoryForm form) {
        Sort sort = Sort.by("id");
        Pageable pageable = param.getPageable(sort);
        Specification<Domitory> spec = buildSpec(form);
        Page<Domitory> page = domitoryService.findAll(spec, pageable);
        return DataGridUtils.buildResult(page);
    }

    /**
     * 动态安全查询
     * @param form
     * @return Specifitation<Domitory>
     */
    public Specification<Domitory> buildSpec(DomitoryForm form) {
        Specification<Domitory> spec = new Specification<Domitory>() {
            private static final long serialVersionUID = -7217537352584153784L;

            @Override
            public Predicate toPredicate(Root<Domitory> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                HashSet<Predicate> rules=new HashSet<>();
                String name = form.getName();
                if (StringUtils.hasText(name)) {
                    Predicate likeName = cb.like(root.get("name"), "%"+name+"%");
                    rules.add(likeName);
                }

                // 充电页面的查询所有宿舍接口
                String keyword = form.getKeyword();
                if(StringUtils.hasText(keyword)) {
                    Predicate likeName = cb.like(root.get("name"), "%"+keyword+"%");
                    rules.add(likeName);
                }


                return cb.and(rules.toArray(new Predicate[rules.size()]));
            }
        };
        return  spec;
    }

    @RequestMapping(value = "/add")
    public Object addDomitory(@RequestBody DomitoryForm form) {
        Domitory domitory = new Domitory();
        BeanUtils.copyProperties(form, domitory);
        domitoryService.save(domitory);
        return AjaxUtils.buildResult("添加宿舍成功");
    }

    @RequestMapping(value = "/delete")
    public Object deleteDomitory(Integer id) {
        domitoryService.deleteById(id);
        return AjaxUtils.buildResult("删除宿舍成功");
    }

    @RequestMapping(value = "/edit")
    public Object editDomitory(@RequestBody DomitoryForm form) {
        Domitory domitory = domitoryService.findById(form.getId());
        BeanUtils.copyProperties(form, domitory, "id");
        domitoryService.save(domitory);
        return AjaxUtils.buildResult("数据更新成功");
    }

}
